home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / dbplus2 / qrymain.pas < prev    next >
Pascal/Delphi Source File  |  1996-09-15  |  5KB  |  180 lines

  1. unit Qrymain;
  2.  
  3.  
  4. interface
  5.  
  6. uses
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, DBTables, StdCtrls, Dblup2, ExtCtrls, DB, Grids, DBGrids;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Panel1: TPanel;
  13.     Query1: TQuery;
  14.     RadioGroup1: TRadioGroup;
  15.     DBLookupComboPlus1: TDBLookupComboPlus;
  16.     Table1: TTable;
  17.     BatchMove1: TBatchMove;
  18.     DataSource1: TDataSource;
  19.     Bevel1: TBevel;
  20.     Memo1: TMemo;
  21.     Label1: TLabel;
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormDestroy(Sender: TObject);
  24.     procedure FormActivate(Sender: TObject);
  25.     procedure RadioGroup1Click(Sender: TObject);
  26.     procedure DBLookupComboPlus1PrepareList(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.     LastRadioItemIndex : Integer;
  30.     function ImFirstInstance : Boolean;
  31.     { The previous instance checking code in this unit comes from the
  32.       post in the libs by Lee Sumner 100067,2216, who extended an original
  33.       post by Pat Ritchey, famous author and Team B'er}
  34.   public
  35.     { Public declarations }
  36.   end;
  37.  
  38. type
  39.   PHWND = ^HWND;
  40.   function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool; export;
  41.  
  42. var
  43.   Form1: TForm1;
  44.  
  45. implementation
  46.  
  47. {$R *.DFM}
  48.  
  49. { Wander the croud looking for dopplegangers }
  50. function EnumFunc(Wnd:HWND; TargetWindow:PHWND): bool;
  51. var
  52.   ClassName : array[0..30] of char;
  53. begin
  54.   Result := true;
  55.   if GetWindowWord(Wnd,GWW_HINSTANCE) = hPrevInst then
  56.   begin
  57.     GetClassName(Wnd,ClassName,30);
  58.     if StrIComp(ClassName,'TApplication') = 0 then
  59.     begin
  60.       TargetWindow^ := Wnd;
  61.       Result := false;
  62.     end;
  63.   end;
  64. end;
  65.  
  66. function TForm1.ImFirstInstance : Boolean;
  67. var
  68.   HW: HWnd;
  69. begin
  70.   result := True;
  71.   if hPrevinst <> 0 then  {If I am already running}
  72.   begin
  73.     EnumWindows(@EnumFunc,longint(@HW));
  74.     if hw <> 0 then
  75.       if IsIconic(HW) then
  76.          ShowWindow(HW,SW_RESTORE)
  77.       else
  78.          BringWindowToTop(HW);
  79.     result := False;
  80.   end;
  81. end;
  82.  
  83. procedure TForm1.FormCreate(Sender: TObject);
  84. begin
  85.   { First make sure this is the only instance of the program }
  86.   If not ImFirstInstance then halt;
  87.   { Create the temporary table }
  88.   { The reason the temporary is explictly created instead of just letting
  89.     the batch move create it with a copy is that there is more control
  90.     over where the table is created. Plus it's self documenting.
  91.     Plus alot of people have been wondering how to create a table from
  92.     inside Delphi so I figured I through it in.}
  93.   with Table1 do
  94.   begin
  95.     Active := False;
  96.     DatabaseName := Session.PrivateDir; {the safe place to put temporary files}
  97.     TableName := 'DropList';
  98.     TableType := ttParadox;
  99.     with FieldDefs do
  100.     begin
  101.       Clear;
  102.       Add('VendorName', ftString, 30, False);
  103.     end;
  104.     with IndexDefs do
  105.     begin
  106.       Clear;
  107.       Add(''{'Field1Index'}, 'VendorName', [ixPrimary, ixUnique]);
  108.       {This seconday index is created just cause I know how much
  109.        you folks like caseinsenstive incremental searches}
  110.       Add('byVendor', 'VendorName',[ixCaseInsensitive]);
  111.     end;
  112.     CreateTable;
  113.   end;
  114.   {Prepare the query}
  115.   Query1.Active := False;
  116.   Query1.Prepare;
  117.   Query1.Active := True;
  118.   { Move the data from the query result to the Temp Table.}
  119.   BatchMove1.Execute;
  120.   { Finish setting the TDBLookupCombosPlus's properties that could
  121.     not be set at design time cause the table didn't exist yet.}
  122.   DBLookupComboPlus1.LookupDisplay := 'VendorName';
  123.   DBLookupComboPlus1.LookupIndex := 'byVendor';
  124.   DBLookupComboPlus1.LookupField := 'VendorName';
  125.   { finally open the table }
  126.   Table1.Exclusive := True;    {make sure, real sure no one else messes with it}
  127.   Table1.open;
  128. end;
  129.  
  130. procedure TForm1.FormDestroy(Sender: TObject);
  131. begin
  132.   If Table1.TableName = 'DropList' then {deals with halt from instance checking} 
  133.   begin
  134.     Table1.Close;
  135.     Table1.DeleteTable;
  136.   end;
  137. end;
  138.  
  139. procedure TForm1.FormActivate(Sender: TObject);
  140. begin
  141.   { Pretty things up }
  142.   Table1.First;
  143.   DBLookupComboPlus1.value := Table1.FieldbyName('VendorName').AsString;
  144.   DBLookupComboPlus1.SetFocus;
  145. end;
  146.  
  147. procedure TForm1.RadioGroup1Click(Sender: TObject);
  148. begin
  149.   DBLookupComboPlus1.SetFocus;
  150. end;
  151.  
  152. procedure TForm1.DBLookupComboPlus1PrepareList(Sender: TObject);
  153. begin
  154.   { First check to make sure query needs to be re-done }
  155.   If LastRadioItemIndex = RadioGroup1.ItemIndex then exit
  156.   else
  157.     LastRadioItemIndex := RadioGroup1.ItemIndex;
  158.   { Redo the query }
  159.  
  160.   Query1.Close;
  161.   case RadioGroup1.ItemIndex of
  162.     0 : begin
  163.           Query1.Params[0].AsBoolean := True;
  164.           Query1.Params[1].AsBoolean := False;
  165.         end;
  166.     1 : begin
  167.           Query1.Params[0].AsBoolean := True;
  168.           Query1.Params[1].AsBoolean := True;
  169.         end;
  170.    end;
  171.   Query1.Open;
  172.   { Get the temp table ready for the new data}
  173.   Table1.EmptyTable; {Didn't need to close it first since Exclusive was set to true below}
  174.   { Move the data from the query result to the Temp Table}
  175.   BatchMove1.Execute;
  176. end;
  177.  
  178.  
  179. end.
  180.